﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IDAL;
using DataMode;
using System.Data;
using System.Text;
using IBLL;
using BLLFactory;
using System.Security.Cryptography;

namespace Face.Web.xwbemo1
{
    public partial class UserInfoEdit : System.Web.UI.Page

    {
        public string imgPath;
        UserinfoIBLL userinfoIBLL = BLLFactory.DataAccess.getUserinfoIBLL();
        IUserInfoEdit userinfoEdit = BLLFactory.DataAccess.GetUser();
        private static IBLL.IAlbum bll = BLLFactory.DataAccess.GetAlbum();
        public string thisUserName;
        public string filePath;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userName"] != null && Session["admin"] != null)
            {
                thisUserName = (Session["userName"].ToString());
            }
            else if (Session["user"] != null)
            {
                thisUserName = (Session["user"].ToString());
            }
            else if (Session["admin"] != null)
            {
                Response.Redirect("/Admin/AdminUserManager.aspx");
            }
            else
            {
                Response.Redirect("/login_and_signup/login.aspx");
            }
            if (!IsPostBack)
            {
                Setbind();
            }
        }

        public string savePath;

        public void Setbind()
        {
            userinfo gui = new userinfo();
            string name = Session["user"].ToString();

            gui = null;
            Console.WriteLine("----------End Add a student");

            gui = userinfoIBLL.getuserinfo(name);

            tb_UserName.Text = gui.username;
            TextBox1.Text = gui.cellphone;
            TextBox2.Text = gui.email;
            string s = gui.birthday.ToString();
            //TextBox3.Text = s;
            TextBox4.Text = gui.password;

            imgPath = gui.user_avatar;
             
        }

        protected void btn_Edit_Click(object sender, EventArgs e)
        {
            userinfo ui = new userinfo();
            string name = Session["user"].ToString();
            ui = userinfoIBLL.getuserinfo(name);


            try
            {
                if (this.albumPicture.HasFile)
                {
                    if (albumPicture.FileContent.Length > 1024 * 1024 * 5)//判断文件大小
                    {
                        UploadStatusLabel.Text = "上传图片过大，请选择5M以下图片上传。";
                    }
                    else
                    {
                        string type = albumPicture.FileName.Substring(albumPicture.FileName.LastIndexOf('.') + 1);
                        if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")//判断文件类型
                        {
                             savePath = SaveFile(albumPicture.PostedFile);//保存文件
                        }
                        else
                        {
                            UploadStatusLabel.Text = "请选择图片格式文件。";
                        }
                    }
                }
                else
                {
                    UploadStatusLabel.Text = "请选择上传图片。";
                }
                

            }
            catch (Exception ex)
            {
                this.UploadStatusLabel.Text = ex.Message;

            }


            string abc = ui.password;
            


            ui.cellphone = TextBox1.Text;
            ui.username = tb_UserName.Text;
            ui.email = TextBox2.Text;
            
            //string date = TextBox3.Text;
            //DateTime newtime = Convert.ToDateTime(date);
            //ui.birthday = newtime;
            bool male = optionsRadios1.Checked;
            bool female = optionsRadios2.Checked;

            

            ui.sex = "male";
            if (female)
            {
                ui.sex = "female";
            }

            if (savePath == null)
            {
                savePath = ui.user_avatar;
            }
            else
            {
                ui.user_avatar = filePath;
            }


            if (TextBox4.Text == "")
            {
                ui.password = abc;
            }
            else
            {
                ui.password = TextBox4.Text;
            }

            userinfoEdit.UpdateUserInfo(ui, name);

            Response.Redirect("/xwbemo1/UserInfoCheck.aspx");
        }



        private string SaveFile(HttpPostedFile file)
        {
            savePath = Request.PhysicalApplicationPath + @"\userAvatar\";
            string fileName = albumPicture.FileName;
            string pathToCheck = savePath + fileName;
            string tempfileName = "";

            if (System.IO.File.Exists(pathToCheck))//判断是否有同名文件存在
            {
                int counter = 2;
                while (System.IO.File.Exists(pathToCheck))
                {
                    tempfileName = counter.ToString() + "_" + fileName;
                    pathToCheck = savePath + tempfileName;
                    counter++;
                }

                fileName = tempfileName;
                UploadStatusLabel.Text = "有同名图片存在，" +
                    "上传图片被另存为 " + fileName;
            }
            else
            {
                UploadStatusLabel.Text = "图片上传成功。";
            }
            savePath += fileName;
            albumPicture.SaveAs(savePath);//保存文件

            filePath = @"\userAvatar\" + fileName;
            



            return fileName;

        }

        protected void btn_logout(object sender, EventArgs e)
        {
            Session.Remove("user");
            Response.Redirect("/login_and_signup/login.aspx");
        }
    }
}